//
//  SCNHomeFootView.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/2/10.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit


class FootViewItem: UICollectionViewCell {


    
    lazy var imgView: UIImageView = {
    
        let imgView = UIImageView(frame: CGRect(x: (self.frame.size.width - 30)/2, y: 10, width: 30, height: 30))
    
        self.backgroundColor = .clear
        
        self.addSubview(imgView)
        
        return imgView
    
    }()
    
    lazy var detailsLabel: UILabel = {
        
        let details = UILabel.init(frame: CGRect(x: 0, y: 45, width: self.frame.size.width, height: 20))
        
        details.textColor = SCNConstants.colorFromHexValue(0x3A3A3A, alpha: 1)
        
        details.font = UIFont.systemFont(ofSize: 13)
        
        details.backgroundColor = .clear
        
        details.textAlignment = .center
        
        self.addSubview(details)
        
        return details
        
    }()
    
}

class SCNHomeFootView: UIView {


    // typealias 可以给任意类型弄个别名
    typealias footAction = (Int) -> Void
    // 给闭包声明变量
    var footActionClosures: footAction?
    
    static let itemIdentifier = "homeFoot"
    
    fileprivate var flowLayout: UICollectionViewFlowLayout!

    fileprivate let dataArray = [["img":"icon_gonggao_new","title":"公告"],["img":"icon_paihang_new","title":"排行"],["img":"icon_gongju_new","title":"工具"],["img":"icon_xuewei_new","title":"学习"],["img":"icon_qiandao_new","title":"签到"]]
    
    
    
    
    lazy var collectionView: UICollectionView = {
        
        
        self.flowLayout = UICollectionViewFlowLayout()
        
        self.flowLayout.minimumLineSpacing = 0
        
        self.flowLayout.minimumInteritemSpacing = 15
        
        self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 0, 15)
        
        
    
        let collection = UICollectionView.init(frame: .zero, collectionViewLayout: self.flowLayout)
        
        collection.delegate = self
        
        collection.dataSource = self
        
        collection.isScrollEnabled = false
        
        collection.backgroundColor = .white

        collection.register(FootViewItem.classForCoder(), forCellWithReuseIdentifier: itemIdentifier)
        
        return collection
        
    
    }()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
 
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.flowLayout.itemSize = CGSize(width: (self.frame.size.width - 90)/5, height: 60)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension SCNHomeFootView: UICollectionViewDelegate, UICollectionViewDataSource {
    
    
    // MARK: UICollectionViewDataSource
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let item = collectionView.dequeueReusableCell(withReuseIdentifier: SCNHomeFootView.itemIdentifier, for: indexPath) as! FootViewItem
        
        let imagename = dataArray[indexPath.item]["img"]
        
        
        item.imgView.image = UIImage(named: imagename!)
        
        item.detailsLabel.text = dataArray[indexPath.item]["title"]
        

        
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        defer {
            collectionView.deselectItem(at: indexPath, animated: true)
        }
        
        footActionClosures?(indexPath.item)
    }
    
}
